package cn.caijiapeng.spt.ctrl;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;

import cn.caijiapeng.spt.bean.Operate;
import cn.caijiapeng.spt.bean.Resp;
import cn.caijiapeng.spt.kit.RequestKit;
import cn.caijiapeng.spt.model.SysAccess;
import cn.caijiapeng.spt.service.SysAccessService;
import cn.caijiapeng.spt.validate.LoginValidate;

public class IndexCtrl extends Controller{
	public void index() {
		render("login/index.html");
	}
	
	public void Main() {
		render("layout.html");
	}
	
	@Before(LoginValidate.class)
	public void login() {
		String username = getPara("username");
		String password = getPara("password");
		String ip = RequestKit.getIpAddr(getRequest());
		String userAgent = RequestKit.getUserAgent(getRequest());
		Operate<SysAccess> access = SysAccessService.srv.login(username, password, ip, userAgent);
		if(access.isOk()) {
			Kv kv = Kv.by("access_token", access.get().get("token"));
			kv.set("expires_in", access.get().get("expires_in"));
			kv.set("login_time", access.get().get("login_time"));
			renderJson(Resp.ok(kv));
			return;
		}
		renderJson(Resp.fail("用户名或密码错误"));
	}
	
	public void captcha() {
		renderCaptcha();
	}
}
